<template>
    <a-modal :width="550"
             :title="title"
             destroyOnClose
             :maskClosable="false"
             v-model="visible">
        <a-form-model
                ref="ruleForm"
                small
                :model="form"
                :rules="rules"
                :label-col="labelCol"
                :wrapper-col="wrapperCol">
            <a-form-model-item label="所属菜单" v-if="actionType === 0">{{form.menuName}}</a-form-model-item>
            <a-form-model-item label="按钮名称" prop="resourceName">
                <a-input placeholder="请输入按钮名称" v-model="form.resourceName" />
            </a-form-model-item>
            <a-form-model-item label="权限值" prop="permission">
                <a-input :disabled="actionType === 1" placeholder="请输入权限值" v-model="form.permission" />
            </a-form-model-item>
            <a-form-model-item label="按钮排序" prop="sortBy">
                <a-input-number class="width-percent-100" :precision="0" placeholder="请输入按钮排序" v-model="form.sortBy" />
            </a-form-model-item>
        </a-form-model>
        <div slot="footer" class="text-c">
            <a-button @click="close">关闭</a-button>
            <a-button type="primary" :loading="loading" @click="sure">确定</a-button>
        </div>
    </a-modal>
</template>

<script>
    export default {
        data () {
            return {
                title: '',
                visible: false,
                loading: false,
                form: {
                    menuName: '',
                    resourceName: '',
                    permission: '',
                    sortBy: '',
                    parentId: '',
                    resourceType: 2,
                    id: '',
                },
                labelCol: { span: 4 },
                wrapperCol: { span: 20 },
                rules: {
                    resourceName: [
                        { required: true, message: '请输入按钮名称', trigger: 'blur' },
                    ],
                    permission: [
                        { required: true, message: '请输入按钮权限值', trigger: 'blur' },
                    ],
                    sortBy: [
                        { required: true, message: '请输入排序', trigger: 'blur' },
                    ],
                },
                actionType: null
            };
        },
        methods: {
            show (data,title,num) {
                this.actionType = num;
                if (num === 0) {
                    this.form = {
                        menuName: data.resourceName,
                        resourceName: '',
                        permission: '',
                        sortBy: '',
                        parentId: data.id,
                        resourceType: 2,
                        id: '',
                    };
                }
                if (num === 1) {
                    this.form = {
                        menuName: data.resourceName,
                        resourceName:  data.resourceName,
                        permission:  data.permission,
                        sortBy: data.sortBy,
                        parentId: data.parentId,
                        resourceType: 2,
                        id: data.id,
                    };
                }
                this.title = title;
                this.visible = true;
            },
            sure () {
                this.$refs.ruleForm.validate(valid => {
                    if (valid) {
                        let api = this.actionType === 0 ? this.$api.permission.addPermission : this.$api.permission.editPermission;
                        let tipMsg = this.actionType === 0 ? '新增成功' : '编辑成功';
                        let params = {
                            resourceName: this.form.resourceName,
                            permission: this.form.permission,
                            sortBy: this.form.sortBy,
                            parentId: this.form.parentId,
                            resourceType: 2,
                            id: this.form.id,
                        };
                        this.editPermissionMenu(api,params,tipMsg);
                    }
                });
            },
            editPermissionMenu (api,data,tipMsg) {
                this.loading = true;
                api(data).then(res => {
                    this.loading = false;
                    if (res && res.code !== 200) {
                        this.$message.error(res.errorMsg);
                        return;
                    }
                    this.$message.success(tipMsg);
                    this.$emit('done');
                    this.close();
                }).catch(err => {
                    this.loading = false;
                    this.$message.error(err.errorMsg);
                });
            },
            close () {
                this.form = {
                    form: {
                        menuName: '',
                        resourceName: '',
                        permission: '',
                        sortBy: '',
                        parentId: '',
                        resourceType: 2,
                        id: '',
                    },
                };
                this.title = '';
                this.actionType = null;
                this.loading = false;
                this.visible = false;
            }
        },
    };
</script>

<style scoped lang="scss">

</style>
